package com.yjx.gulimall.coupon.service.impl;

import com.yjx.gulimall.common.to.SkuReductionTO;
import com.yjx.gulimall.common.vo.product.MemberPrice;
import com.yjx.gulimall.coupon.entity.SkuLadderEntity;
import com.yjx.gulimall.coupon.service.MemberPriceService;
import com.yjx.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjx.gulimall.common.utils.PageUtils;
import com.yjx.gulimall.common.utils.Query;

import com.yjx.gulimall.coupon.dao.SkuFullReductionDao;
import com.yjx.gulimall.coupon.entity.SkuFullReductionEntity;
import com.yjx.gulimall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveInfo(SkuReductionTO skuReductionTO) {

        Long skuId = skuReductionTO.getSkuId();
        //保存会员价
        List<MemberPrice> memberPriceList = skuReductionTO.getMemberPrice();
        //过滤 会员价最低为0.01
         memberPriceList = memberPriceList.stream().filter(memberPrice1 -> memberPrice1.getPrice().compareTo(new BigDecimal("0")) == 1).collect(Collectors.toList());
        memberPriceService.saveBatchInfo(skuId, memberPriceList);

        //保存打折信息
        BigDecimal discount = skuReductionTO.getDiscount();
        int fullCount = skuReductionTO.getFullCount();
        if (fullCount > 0 && !(discount.compareTo(new BigDecimal("1")) == 0)) {
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            skuLadderEntity.setSkuId(skuId);
            skuLadderEntity.setFullCount(fullCount);
            skuLadderEntity.setDiscount(discount);
            skuLadderEntity.setAddOther(skuReductionTO.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }

        //保存满减信息
        if (skuReductionTO.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            skuFullReductionEntity.setSkuId(skuId);
            skuFullReductionEntity.setFullPrice(skuReductionTO.getFullPrice());
            skuFullReductionEntity.setReducePrice(skuReductionTO.getReducePrice());
            skuFullReductionEntity.setAddOther(skuReductionTO.getPriceStatus());
            this.save(skuFullReductionEntity);
        }

    }

}